# import jusmargui
from PySide2.QtWidgets import (
    QApplication,
    QWidget,
    QTableWidgetItem,
    QTableWidget,
    QHeaderView,
    QAbstractItemView,
    QVBoxLayout,
    QTabWidget,
    QGroupBox,
    QLabel,
    QLineEdit,
    QHBoxLayout,
    QGridLayout,
    QComboBox,
    QPushButton,
    QListWidget,
    QDialog,
    QButtonGroup,
    QRadioButton,
    QCheckBox,
    QListWidgetItem,
    QMessageBox,
    QGraphicsColorizeEffect,
    QFileDialog,
)
from PySide2.QtCore import Qt, Signal
from PySide2.QtGui import QPixmap, QDoubleValidator, QIntValidator, QImage
import sys
import csv
import os
import json
import subprocess
import copy
from .. import config as Config
from ..utils import Utils
from ..dlg_model_params import DlgModeResetCls
from ..custom_widget import LineEditStr


class DlgImportBdf(DlgModeResetCls):
    importBdf_save_signal = Signal(object)

    def __init__(self, parent=None, params={}):
        super().__init__(parent)
        title = f"导入bdf-{params.get('title')or''}"
        self.setWindowTitle(title)
        # self.resize(600, -1)
        self.setMinimumWidth(500)

        layout = QVBoxLayout(self)
        # vLayout = QVBoxLayout()
        gridLayout = QGridLayout()

        # hLayout = QHBoxLayout()
        self.bdfEdit = LineEditStr()
        # hLayout.addWidget(self.bdfEdit)
        importBtn = QPushButton("导入")
        gridLayout.addWidget(self.bdfEdit, 0, 0)
        gridLayout.addWidget(importBtn, 0, 1)
        widget = QWidget()
        widget.setLayout(gridLayout)
        # layout.addLayout(hLayout)
        layout.addWidget(widget)

        btnLayout = QHBoxLayout()
        cancelBtn = QPushButton("取消")
        okBtn = QPushButton("确定")

        btnLayout.setAlignment(Qt.AlignCenter)
        btnLayout.addWidget(okBtn)
        btnLayout.addWidget(cancelBtn)
        layout.addLayout(btnLayout)

        importBtn.clicked.connect(self.on_importBtn_clicked)
        okBtn.clicked.connect(self.on_okBtn_clicked)
        cancelBtn.clicked.connect(self.close)

        # layout.addStretch(1)
        # layout.addLayout(vLayout)
        # layout.addStretch(1)
        self.setLayout(layout)

        self.cacheFile = Config.cachePath + "importBdf.json"
        self.updateUI(params.get("data") or {})

    def on_importBtn_clicked(self):
        # 创建QFileDialog对话框实例
        file_dialog = QFileDialog()
        file_dialog.setNameFilter("DBF files (*.bdf)")
        # 设置对话框标题
        file_dialog.setWindowTitle("选择bdf文件")
        # 显示对话框并获取用户选择的文件
        if file_dialog.exec_():
            # 用户点击了“打开”或“保存”，获取选中的文件路径
            file_path = file_dialog.selectedFiles()[0]
            self.bdfEdit.setText(file_path)

    def on_okBtn_clicked(self):
        # self.saveData(self.cacheFile)
        self.close()
        res = {"path": self.bdfEdit.text(), "type": "bdf"}
        self.importBdf_save_signal.emit(res)

    def updateUI(self, data):
        if data:
            self.bdfEdit.setValue(data.get("path"))

    def saveData(self, file):
        data = {"path": self.bdfEdit.text()}
        Utils.write_json(file, data)


# if __name__ == "__main__":
#     app = QApplication(sys.argv)
#     window = ImportBdfDialog()
#     window.show()
#     sys.exit(app.exec_())
